package com.bharatonjava.swift.web.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.springframework.context.ApplicationContext;

import com.bharatonjava.swift.domain.Enumeration;
import com.bharatonjava.swift.service.EnumerationService;
import com.bharatonjava.swift.util.ApplicationContextUtils;

public class EnumerationConverter implements Converter{
	
	private EnumerationService enumerationService;
	private ApplicationContext applicationContext;
	
	public void setEnumerationService(EnumerationService enumerationService) {
		this.enumerationService = enumerationService;
		this.applicationContext = ApplicationContextUtils.getApplicationContext();
		enumerationService = (EnumerationService) applicationContext.getBean("enumerationsService");
	}

	@Override
	public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String arg2) {
		System.out.println("getAsObject : "+ arg2);
		Enumeration enumeration = null;
		this.applicationContext = ApplicationContextUtils.getApplicationContext();
		enumerationService = (EnumerationService) applicationContext.getBean("enumerationsService");
		System.out.println("applicationContext: "+applicationContext);
		System.out.println("enumerationService: "+ enumerationService);
		enumeration = this.enumerationService.getEnumerationByValue(arg2);
		return enumeration;
	}

	@Override
	public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object object) {
		System.out.println(object);
		Enumeration enumeration = (Enumeration) object;
		return enumeration.getEnumValue();
	}

}
